﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.Training
{
	/// <summary>
	/// 控制类
	/// </summary>
	/// <![CDATA[2014-8-24 14:06:38]]>
	public class Component : DLL.ComponentBase
	{
		#region 属性
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-8-24 14:06:38]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-8-24 14:06:38]]>
		private Config clsConfig = new Config();		// 配置类对象
		private TrainingOrganization.Component clsTrainingOrganizationComp;
		private TrainingSort.Component clsTrainingSortComp;
		private TrainingExperience.Component clsTrainingExperienceComp;
		#endregion 属性

		#region 构造函数
		/// <summary>
		/// 控制类构造函数
		/// </summary>
		/// <![CDATA[2014-8-24 14:06:38]]>
		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);
			clsData = clsDatabase;
			clsTrainingOrganizationComp = new TrainingOrganization.Component(page);
			clsTrainingSortComp = new TrainingSort.Component(page);
			clsTrainingExperienceComp = new TrainingExperience.Component(page);

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nSortID", clsDatabase.SORTID, clsDatabase.listStructure[clsDatabase.SORTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strName", clsDatabase.NAME, clsDatabase.listStructure[clsDatabase.NAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nDepartmentID", clsDatabase.DEPARTMENTID, clsDatabase.listStructure[clsDatabase.DEPARTMENTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strStartDate", clsDatabase.STARTDATE, clsDatabase.listStructure[clsDatabase.STARTDATE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strEndDate", clsDatabase.ENDDATE, clsDatabase.listStructure[clsDatabase.ENDDATE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nUserID", clsDatabase.USERID, clsDatabase.listStructure[clsDatabase.USERID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nUserDepartmentID", clsDatabase.USERDEPARTMENTID, clsDatabase.listStructure[clsDatabase.USERDEPARTMENTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strDateTime", clsDatabase.DATETIME, clsDatabase.listStructure[clsDatabase.DATETIME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strTeachers", clsDatabase.TEACHERS, clsDatabase.listStructure[clsDatabase.TEACHERS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nType", clsDatabase.TYPE, clsDatabase.listStructure[clsDatabase.TYPE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nState", clsDatabase.STATE, clsDatabase.listStructure[clsDatabase.STATE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strAddress", clsDatabase.ADDRESS, clsDatabase.listStructure[clsDatabase.ADDRESS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strContent", clsDatabase.CONTENT, clsDatabase.listStructure[clsDatabase.CONTENT].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nOrganizationID", clsDatabase.ORGANIZATIONID, clsDatabase.listStructure[clsDatabase.ORGANIZATIONID].strName));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nUserID", clsDatabase.listStructure[clsDatabase.USERID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nState", clsDatabase.listStructure[clsDatabase.STATE].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strStates", clsDatabase.listStructure[clsDatabase.STATE].strName, "IN"));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strStartDate", clsDatabase.listStructure[clsDatabase.STARTDATE].strName, "<=", true));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strEndDate", clsDatabase.listStructure[clsDatabase.ENDDATE].strName, "<=", true));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strQueryStartDate", clsDatabase.listStructure[clsDatabase.STARTDATE].strName, ">=", true));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strQueryEndDate", clsDatabase.listStructure[clsDatabase.ENDDATE].strName, "<=", true));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nType", clsDatabase.listStructure[clsDatabase.TYPE].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nSort", clsDatabase.listStructure[clsDatabase.SORTID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strName", clsDatabase.listStructure[clsDatabase.NAME].strName, "LIKE"));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nDepartmentID", clsDatabase.listStructure[clsDatabase.DEPARTMENTID].strName, "="));
		}
		#endregion 构造函数

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-8-24 14:06:38]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		protected override TonyNet4V1.EntityBase DataToEntity(System.Data.DataRow dr)
		{
			Entity clsEntity = new Entity();
			TrainingOrganization.Entity clsTrainingOrganization = new TrainingOrganization.Entity();
			Department.Entity clsDepartment = new Department.Entity();
			Department.Component clsDepartmentComp = new Department.Component(page);

			clsEntity = (Entity) base.DataToEntity(dr);
			clsEntity.StateName = clsConfig.strArrTrainingState[clsEntity.State];

			clsTrainingOrganization = clsTrainingOrganizationComp.GetOne(clsEntity.OrganizationID);
			if (clsTrainingOrganization != null)
			{
				clsEntity.OrganizationName = clsTrainingOrganization.Name;
			}

			clsDepartment = clsDepartmentComp.GetOne(clsEntity.DepartmentID);
			if (clsDepartment != null)
			{
				clsEntity.DepartmentName = clsDepartment.DepartmentName;
			}
			else
			{
				clsEntity.DepartmentName = "无";
			}

			return clsEntity;
		}

		#region 查询
		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-8-24 14:06:38]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-8-24 14:06:38]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <![CDATA[2014-8-24 14:06:38]]>
		/// <returns></returns>
		public List<Entity> Query(QueryEntity clsQuery)
		{
			return ConvertListEntityType(base.Query(clsQuery));
		}
		#endregion 查询

		/// <summary>
		/// 查询本人上传的培训班
		/// </summary>
		/// <returns></returns>
		public List<Entity> QueryMySelfTraining(int nUserID)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nUserID = nUserID;

			return Query(clsQuery);
		}
		/// <summary>
		/// 查询本人上传的处在创办阶段的培训班
		/// </summary>
		/// <returns></returns>
		public List<Entity> QueryMySelfBulidTraining(int nUserID)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nUserID = nUserID;
			clsQuery.nState = clsConfig.nTrainingState_Bulid;

			return Query(clsQuery);
			
		}
		/// <summary>
		/// 查询本人添加的可以上传课件的培训班（培训班处在创办和报名阶段）
		/// </summary>
		/// <param name="nUserID"></param>
		/// <returns></returns>
		public List<Entity> QueryMySelfCanAddCoursewareTraining(int nUserID)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nUserID = nUserID;
			clsQuery.strStates = clsConfig.nTrainingState_Bulid.ToString() + ", " + clsConfig.nTrainingState_Apply.ToString();

			return Query(clsQuery);
		}
		/// <summary>
		/// 查询所有处在报名阶段的培训班
		/// </summary>
		/// <returns></returns>
		public List<Entity> QueryApplyTraining()
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nState = clsConfig.nTrainingState_Apply;

			return Query(clsQuery);
		}

		/// <summary>
		/// 查询所有处在开课状态的培训班
		/// </summary>
		/// <returns>所有已经开课的培训班</returns>
		public List<Entity> QueryCommencementTraining()
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nState = clsConfig.nTrainingState_Commencement;
			//clsQuery.nType = clsConfig.nTrainingType_OnLine;

			return Query(clsQuery);
		}

		/// <summary>
		/// 添加
		/// </summary>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Add(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsAdd = new Entity();

			clsAdd = (Entity)clsEntity;
			clsAdd.DateTime = System.DateTime.Now.ToString();
			clsAdd.State = clsConfig.nTrainingState_Bulid;

			return base.Add(clsAdd);
		}

		/// <summary>
		/// 指定一个培训班开始报名（没用了）
		/// </summary>
		/// <param name="nTrainingID"></param>
		/// <returns></returns>
		public bool StartApply(int nTrainingID)
		{
			Entity clsEntity = new Entity();

			clsEntity = GetOne(nTrainingID);

			clsEntity.State = clsConfig.nTrainingState_Apply;

			return Modify(clsEntity);
		}

		#region 根据当前日期，把需要开课的培训班开课
		/// <summary>
		/// 根据当前日期，把需要开课的培训班开课
		/// </summary>
		/// <returns></returns>
		public bool TrainingCommencementState()
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			//clsQuery.nState = clsConfig.nTrainingState_Apply;
			clsQuery.nState = clsConfig.nTrainingState_Bulid;
			clsQuery.strStartDate = System.DateTime.Now.ToShortDateString();
			list = Query(clsQuery);

			for (int i = 0; i < list.Count; i++)
			{
				list[i].State = clsConfig.nTrainingState_Commencement;
				Modify(list[i]);
			}

			return true;
		}
		#endregion 根据当前日期，把需要开课的培训班开课

		#region 根据当前日期，把需要结束课程的培训班结束
		/// <summary>
		/// 根据当前日期，把需要结束课程的培训班结束
		/// </summary>
		/// <returns></returns>
		public bool TrainingEndState()
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();
			TrainingPerson.Component clsTrainingComp = new TrainingPerson.Component(page);
			List<TrainingPerson.Entity> listTrainingPerson = new List<TrainingPerson.Entity>();
			

			clsQuery.nState = clsConfig.nTrainingState_Commencement;
			clsQuery.strEndDate = System.DateTime.Now.ToShortDateString();
			list = Query(clsQuery);

			for (int i = 0; i < list.Count; i++)
			{
				list[i].State = clsConfig.nTrainingState_End;
				Modify(list[i]);

				listTrainingPerson = clsTrainingComp.QueryByTrainingID(list[i].ID);

				for (int j = 0; j < listTrainingPerson.Count; j++)
				{
					TrainingExperience.Entity clsAddTrainingExperience = new TrainingExperience.Entity();

					clsAddTrainingExperience.PersonID = Convert.ToInt32(listTrainingPerson[j].PersonID);
					clsAddTrainingExperience.PersonName = listTrainingPerson[j].PersonName;
					clsAddTrainingExperience.StartDate = list[i].StartDate;
					clsAddTrainingExperience.EndDate = list[i].EndDate;
					clsAddTrainingExperience.Address = list[i].Address;
					clsAddTrainingExperience.Content = list[i].Content;
					clsAddTrainingExperience.Organization = list[i].OrganizationName;
					clsAddTrainingExperience.Sort = clsTrainingSortComp.QueryPxlxID(list[i].SortID);
					clsAddTrainingExperience.PersonDepartmentID = listTrainingPerson[j].PersonDepartmentID;

					if (clsTrainingExperienceComp.Add(clsAddTrainingExperience))
					{
					}
					else
					{
						//TonyNet4V1.ShowMessageAlert.ShowMessage(page, "添加培训经历出错，请联系管理员！");
						TonyNet4V1.ShowMessageAlert.ShowError(page, "培训" +listTrainingPerson[j].ID+ "添加培训经历出错，请联系管理员！");
						break;
					}
				}
			}

			return true;
		}
		#endregion 根据当前日期，把需要结束课程的培训班结束

	}
}
